package com.jerry.chat.service;

import com.gexin.rp.sdk.base.impl.SingleMessage;
import com.gexin.rp.sdk.base.impl.Target;
import com.gexin.rp.sdk.http.IGtPush;
import com.gexin.rp.sdk.template.NotificationTemplate;
import com.gexin.rp.sdk.template.style.Style0;
import com.jerry.chat.dao.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class JPushServiceImpl implements JPushService {

    // STEP1：获取应用基本信息
    private static String appId = "8fnOUNy8ae5J5TGTYf2Za4";
    private static String appKey = "aO4qHNCWs56578apssjwA4";
    private static String masterSecret = "nVlqN4qwzz9S6SuslsnWdA";
    // 如果需要使用HTTPS，直接修改url即可
    //private static String url = "https://api.getui.com/apiex.htm";
    private static String url = "http://api.getui.com/apiex.htm";
    static int i = 0;

    @Autowired
    private UserMapper userMapper;

    @Override
    public void pushMessage(String cid, String title, String content, String transmissionContent) {
        IGtPush push = new IGtPush(url, appKey, masterSecret);

        Style0 style = new Style0();
        // STEP2：设置推送标题、推送内容
        style.setTitle(title);
        style.setText(content);
        style.setLogo("push.png");  // 设置推送图标
        // STEP3：设置响铃、震动等推送效果
        style.setRing(true);  // 设置响铃
        style.setVibrate(true);  // 设置震动
        style.setClearable(true);

        // STEP4：选择通知模板
        NotificationTemplate template = new NotificationTemplate();
        template.setAppId(appId);
        template.setAppkey(appKey);
        template.setStyle(style);
        template.setNotifyid(i++);
        template.setTransmissionType(1);
        template.setTransmissionContent("{\"senderId\":" + transmissionContent + "}");

        // STEP5：定义"AppMessage"类型消息对象,设置推送消息有效期等推送参数
//        List<String> appIds = new ArrayList<String>();
//        appIds.add(appId);
//        AppMessage message = new AppMessage();
//        message.setData(template);
//        message.setAppIdList(appIds);
//        message.setOffline(true);
//        message.setOfflineExpireTime(2 * 1000);  // 时间单位为毫秒
        var message = new SingleMessage();
        message.setData(template);
        message.setOffline(true);
        message.setOfflineExpireTime(60 * 1000);

        Target target = new Target();
        target.setAppId(appId);
        target.setClientId(cid);
        push.pushMessageToSingle(message, target);

        // STEP6：执行推送
        //IPushResult ret = push.pushMessageToApp(message);
    }
}
